package linear

import "fmt"

/**
单链表的实现方式：
	1、带头节点
	2、不带头节点
 */
type Node struct {
	Value string
	pNext *Node
}

type List struct {
	Head *Node
	Size int
}

func NewList() *List {
	return &List{
		Head: &Node{Value: "", pNext: nil},
		Size: 0,
	}
}

func (li *List) AddNodeToTail(node *Node) {
	if li.Head == nil {
		fmt.Errorf("head is nil")
		return
	}

	pNode := li.Head
	for {
		if pNode.pNext == nil {
			pNode.pNext = node
			break
		}

		pNode = pNode.pNext
	}

	li.Size++
}

func (li *List) AddNodeToHead(node *Node) {
	if li.Head == nil {
		fmt.Errorf("head is nil")
		return
	}

	pNode := li.Head.pNext

	li.Head.pNext = node
	node.pNext = pNode

	li.Size++
}

func (li *List) Print() {
	if li.Head == nil {
		fmt.Errorf("head is nil")
		return
	}

	fmt.Println("list size:", li.Size)
	node := li.Head
	for {
		node = node.pNext
		if node == nil {
			break
		}
		fmt.Println(node)
	}
}
